{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "from openai import OpenAI\n",
    "client = OpenAI(base_url='http://localhost:11434/v1', api_key='ollama')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "SYSTEM_PROMPT = \"\"\"\n",
    "按照如下格式生成：\n",
    "<think>\n",
    "...\n",
    "</think>\n",
    "<answer>\n",
    "...\n",
    "</answer>\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "这是一个基于Python的解决方案，使用BeautifulSoup库来解析HTML。这个例子假设你已经有了一个包含\"think\"和\"answer\"部分的HTML字符串：\n",
      "\n",
      "```python\n",
      "from bs4 import BeautifulSoup\n",
      "\n",
      "# 你的HTML字符串\n",
      "html_string = \"<div>...</div><span>...</span>\"\n",
      "\n",
      "# 使用BeautifulSoup解析HTML\n",
      "soup = BeautifulSoup(html_string, 'html.parser')\n",
      "\n",
      "# 找到所有的\"think\"和\"answer\"部分\n",
      "thinks = soup.findAll('div')\n",
      "answers = soup.findAll('span')\n",
      "\n",
      "# 检查是否有相同数量的\"think\"和\"answer\"部分\n",
      "if len(thinks) != len(answers):\n",
      "    print(\"Error: Number of think and answer sections do not match.\")\n",
      "else:\n",
      "    # 生成格式化的字符串\n",
      "    for think, answer in zip(thinks, answers):\n",
      "        formatted_string = f\"<think>{think.text}</think><answer>{answer.text}</answer>\"\n",
      "        print(formatted_string)\n",
      "```\n",
      "\n",
      "这个脚本会找到所有的\"div\"和\"span\"标签，并将它们的内容放在相应的格式化字符串中。如果\"think\"和\"answer\"部分的数量不匹配，它会打印一个错误消息。\n",
      "\n",
      "请注意，这只是一个基本示例，你可能需要根据你的具体需求进行调整。例如，如果你有多个相同类型的\"think\"或\"answer\"部分，或者它们的位置在HTML中不是固定的等。\n",
      "\n"
     ]
    }
   ],
   "source": [
    "res = client.chat.completions.create(\n",
    "    model=\"deepseek-coder:6.7b\",\n",
    "    messages=[{\"role\": \"system\", \"content\": SYSTEM_PROMPT}, {\"role\": \"user\", \"content\": '树下一只猴？树上骑个猴？请问一共几只猴？'}],\n",
    "    temperature=0\n",
    ")\n",
    "print(res.choices[0].message.content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_openai import ChatOpenAI\n",
    "llm = ChatOpenAI(temperature=0, base_url='http://localhost:11434/v1', api_key='ollama', model='deepseek-coder:6.7b')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AIMessage(content='对不起，我无法回答这个问题。作为一个AI语言模型，我不具备视觉感知能力，也不能理解或计算物体的数量。我的主要功能是提供编程和技术相关的帮助。如果你有关于这方面的问题，欢迎随时向我提问。\\n', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 62, 'prompt_tokens': 66, 'total_tokens': 128, 'completion_tokens_details': None, 'prompt_tokens_details': None}, 'model_name': 'deepseek-coder:6.7b', 'system_fingerprint': 'fp_ollama', 'finish_reason': 'stop', 'logprobs': None}, id='run-561d179d-54f7-427e-807c-5e6a01d7bec8-0', usage_metadata={'input_tokens': 66, 'output_tokens': 62, 'total_tokens': 128, 'input_token_details': {}, 'output_token_details': {}})"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 调用 llm.invoke 方法时传递系统提示\n",
    "llm.invoke([\n",
    "    {\"role\": \"system\", \"content\": SYSTEM_PROMPT},\n",
    "    {\"role\": \"user\", \"content\": '树下一只猴？树上骑个猴？请问一共几只猴？'}\n",
    "])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
